package leetcode.剑指offer.剑指II;

import org.junit.Test;

import java.util.*;

/**
 * @author ：zsy
 * @date ：Created 2021/9/25 15:01
 * @description：
 */
public class 剑指_II_005_单词长度的最大乘积 {
    @Test
    public void test() {
        Solution solution = new Solution();
    }

    class Solution {
        public int maxProduct(String[] words) {
            HashMap<Integer, int[]> map = new HashMap<>();
            int index = 0;
            for (String word : words) {
                int[] counts = new int[26];
                for (int i = 0; i < word.length(); i++) {
                    counts[word.charAt(i) - 'a']++;
                }
                map.put(index++, counts);
            }
            int max = 0;
            for (int i = 0; i < words.length; i++) {
                for (int j = i + 1; j < words.length; j++) {
                    int[] counts1 = map.get(i);
                    int[] counts2 = map.get(j);
                    boolean flag = true;
                    for (int k = 0; k < 26; k++) {
                        if (counts1[k] != 0 && counts2[k] != 0) {
                            flag = false;
                            break;
                        }
                    }
                    if (flag) {
                        max = Math.max(max, words[i].length() * words[j].length());
                    }
                }
            }
            return max;
        }
    }
}
